package com.jy.adscm.core.repository.config;


import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.jy.adscm.core.repository.properties.DataSourceSqlServerProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


/**
 *   ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 * @author yyy
 * @ClassName MySqlFollowerDataBaseConfig
 * @Description mysql 从数据源的配置类
 * @date 2018/7/27 18:42
 */
@Configuration
@PropertySource(value = {"classpath:db.properties"})
@MapperScan(basePackages = {"com.jy.adscm.*.dao.mysqlFollower","com.jy.adscm.*.mapper.mysqlFollower"}, sqlSessionTemplateRef = "mysqlFollowerSqlSessionTemplate")
public class MySqlFollowerDataBaseConfig {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${mybatis.mapper-mysql-follower-locations}")
    String mapperLocations;
    @Value("${mybatis.typeAliasesPackage}")
    String typeAliasesPackage;


    @Bean(name = "mysqlFollowerBaseDataSource")
    public DataSource sqlServerBaseDataSource(DataSourceSqlServerProperties dataSourceSqlServerProperties){
        logger.info("初始化mysqlFollower的Druid数据源 开始...");
        DruidXADataSource dataSource = new DruidXADataSource();
        BeanUtils.copyProperties(dataSourceSqlServerProperties,dataSource);
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(dataSource);
        xaDataSource.setUniqueResourceName("mysqlFollowerBaseDataSource");
        logger.info("初始化mysqlFollower的Druid数据源 成功 ... "+xaDataSource);
        return xaDataSource;
    }

    @Bean(name = "mysqlFollowerSqlSessionFactory")
    public SqlSessionFactory sqlServerSqlSessionFactory(@Qualifier("mysqlFollowerBaseDataSource") DataSource sqlServerBaseDataSource)
            throws Exception {
        logger.info("初始化mysqlFollower的SqlSessionFactory 开始...");
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(sqlServerBaseDataSource);
        bean.setTypeAliasesPackage(typeAliasesPackage);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        return bean.getObject();
    }

    @Bean(name = "mysqlFollowerSqlSessionTemplate")
    public SqlSessionTemplate sqlServerSqlSessionTemplate(@Qualifier("mysqlFollowerSqlSessionFactory") SqlSessionFactory sqlServerSqlSessionFactory) throws Exception {
        logger.info("初始化mysqlFollower的SqlSessionTemplate 开始...");
        return new SqlSessionTemplate(sqlServerSqlSessionFactory);
    }

}






//
///**
// * @author yyy
// * @ClassName SqlServerDataBaseConfig
// * @Description sqlserver数据库相关配置
// * @date 2018/7/27 16:44
// */
//@Configuration
//@PropertySource(value = {"classpath:db.properties"})
//@MapperScan(basePackages = {"com.jy.adscm.core.dao.sqlServer"/*,"com.jy.adscm.*.dao.sqlServer.*"*/},sqlSessionTemplateRef = "sqlServerSqlSessionTemplate")
//public class SqlServerDataBaseConfig {
//
//
//    Logger logger = LoggerFactory.getLogger(SqlServerDataBaseConfig.class);
//
//    @Value("${mybatis.mapper-sqlServer-locations}")
//    String mapperLocations;
//
//
//
//
//
////    /**
////     * 数据源2：sqlserver数据源
////     * @return
////     */
////    @Bean(name = "sqlServerBaseDataSource")
////    @Qualifier("sqlServerBaseDataSource")
////    @ConfigurationProperties(prefix = "spring.datasource.sqlServer")
////    public DataSource sqlServerBaseDataSource(){
////        logger.info("初始化sqlServer的Druid数据源 开始...");
//////        DataSource dataSource = DruidDataSourceBuilder.create().build();//new DruidDataSource();//
//////        DataSource dataSource = new DruidDataSourceAutoConfigure().dataSource();
//////        logger.info("初始化sqlServer的Druid数据源 成功 ... "+dataSource);
////
//////        return dataSource;
////        return  new DruidXADataSource();//new DruidDataSource();
////    }
//
//
//    @Bean(name = "sqlServerSqlSessionFactory")
//    @ConfigurationProperties(prefix = "mybatis")
//    public SqlSessionFactory sqlServerSqlSessionFactory(@Qualifier("sqlServerBaseDataSource") DataSource sqlServerBaseDataSource) throws Exception {
//        logger.info("初始化sqlServer的 SqlSessionFactory ...");
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        // bean.setConfiguration(getConfiguration());
//        bean.setDataSource(sqlServerBaseDataSource);
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
//        return bean.getObject();
//    }
//
//    @Bean(name = "sqlServerSqlSessionTemplate")
//    public SqlSessionTemplate sqlServerSqlSessionTemplate(@Qualifier("sqlServerSqlSessionFactory") SqlSessionFactory sqlServerSqlSessionFactory) throws Exception {
//        logger.info("初始化sqlServer的 SqlSessionTemplate ...");
//        return new SqlSessionTemplate(sqlServerSqlSessionFactory);
//    }
////    @Bean(name = "sqlServerTransactionManager")
////    public DataSourceTransactionManager sqlServerTransactionManager(@Qualifier("sqlServerBaseDataSource") DataSource sqlServerBaseDataSource) {
////        logger.info("初始化sqlServer的 TransactionManager ...");
////        return new DataSourceTransactionManager(sqlServerBaseDataSource);
////    }
//}
